/*Copyright (c) 2020 Orca Data Technology(Xi'an)CO.LTD. All rights reserved. * Create by Jisr on 2021/1/21 10:08 上午*/

const openDB = (collectName: string) => {
    return new Promise((resolve, reject) => {
        const request = window.indexedDB.open('web-man', 1);
        request.onsuccess = function (e: any) {
            resolve(e.target.result);
        };
        request.onerror = function (e: any) {
            reject(e.target.result);
        };
        request.onupgradeneeded = function (e: any) {
            const db = e.target.result;
            if (!db.objectStoreNames.contains(collectName)) {
                if (collectName === 'urls') {
                    const objectStore = db.createObjectStore(collectName, { keyPath: 'id', autoIncrement: true });
                    objectStore.createIndex('url', 'url', { unique: false });
                    objectStore.createIndex('parent', 'parent', { unique: false });
                    objectStore.createIndex('env', 'env', { unique: false });
                    objectStore.createIndex('group', 'group', { unique: false });
                    objectStore.createIndex('method', 'method', { unique: false });
                    objectStore.createIndex('useTime', 'useTime', { unique: false });
                    objectStore.createIndex('menuIndex', 'menuIndex', { unique: false });
                    objectStore.createIndex('docId', 'docId', { unique: false });
                    objectStore.createIndex('isCollection', 'isCollection', { unique: false });
                } else {
                    console.error(`web-man数据库中未发现表:${collectName}`);
                }
            }
            resolve(e.target.result);
        };
    });
};
const db = {
    onPull: (collectName: string, index?: string) => {
        return new Promise((resolve, reject) => {
            openDB(collectName)
                .then((db: any) => {
                    if (index) {
                        //
                    } else {
                        const request = db.transaction(collectName).objectStore(collectName).openCursor();
                        const result: any = [];
                        request.onsuccess = function (res: any) {
                            if (res.target.result) {
                                const item = res.target.result;
                                result.push(item.value);
                                item.continue();
                            } else {
                                db.close();
                                resolve(result.reverse());
                            }
                        };
                    }
                })
                .catch((db) => {
                    throw db;
                });
        });
    },
    onPush: (collectName: string, data: any) => {
        return new Promise((resolve, reject) => {
            openDB(collectName)
                .then((db: any) => {
                    const request = db.transaction([collectName], 'readwrite').objectStore(collectName).add(data);
                    request.onsuccess = function () {
                        db.close();
                        resolve();
                    };
                    request.onerror = function (err: any) {
                        reject(err);
                        db.close();
                        throw err;
                    };
                })
                .catch((err) => {
                    reject(err);
                    throw err;
                });
        });
    },
    onUpdate: (collectName: string, index: string, data: any) => {
        console.log('onUpdate', collectName, index, data);
    },
    onDelete: (collectName: string, index: string) => {
        console.log('onDelete', collectName, index);
    }
};

export default db;
